//
//  MKUITableViewSectionModel.m
//  MKUI
//
//  Created by sushangjin on 14-4-1.
//  Copyright (c) 2014年 sushangjin.com. All rights reserved.
//

#import "MKUITableViewSectionModel.h"
#import "MKUITableViewCellModel.h"
#import "MKUITableViewModel.h"
#import "MKUITableViewSectionViewProtocol.h"
#import "MKUITableViewSectionView.h"

@implementation MKUITableViewSectionModel
- (id)init{
	if (self=[super init]) {
		self.headViewClass = [MKUITableViewSectionView class];
		self.footViewClass = [MKUITableViewSectionView class];
	}
	return self;
}
- (id)copyWithZone:(NSZone *)zone{
    MKUITableViewSectionModel *obj = [super copyWithZone:zone];
    obj.indexTitle = self.indexTitle;
    obj.headViewClass = self.headViewClass;
    obj.footViewClass = self.footViewClass;
    obj.showHeadView = self.showHeadView;
    obj.showFootView= self.showFootView;
    obj.showDefaultHeadView = self.showDefaultHeadView;
    obj.showDefaultFootView = self.showDefaultFootView;
    obj.headViewHeight = self.headViewHeight;
    obj.footViewHeight = self.footViewHeight;
    obj.headTitle = self.headTitle;
    obj.footTitle = self.footTitle;
    obj.tableViewModel = self.tableViewModel;
    obj.whenShowHeadView = [self.whenShowHeadView copy];
    obj.whenShowFootView = [self.whenShowFootView copy];
    return obj;
}
- (UITableView *)tableView{
	return self.tableViewModel.tableView;
}
- (MKUITableViewModel *)tableViewModel{
	MKCollectionModel *collectionModel = [super collectionModel];
	if([collectionModel isKindOfClass:[MKUITableViewModel class]]){
		return (MKUITableViewModel *)collectionModel;
	}
	return nil;
}
- (void)setTableViewModel:(MKUITableViewModel *)tableViewModel{
	self.collectionModel = tableViewModel;
}
- (__kindof MKUITableViewCellModel *)cellModelAtIndex:(NSInteger)index{
	MKCollectionCellModel *cellModel = [super cellModelAtIndex:index];
	if([cellModel isKindOfClass:[MKUITableViewCellModel class]]){
		return (MKUITableViewCellModel *)cellModel;
	}
	return nil;
 }
- (id)initWithBlankHeadView:(CGFloat)height{
	if(self=[self init]){
		[self showDefaultHeadViewWithHeight:height];
		[self showDefaultFootViewWithHeight:0.1];
	}
	return self;
}
- (id)initWithBlankFootView:(CGFloat)height{
	if(self=[self init]){
		[self showDefaultHeadViewWithHeight:0.1];
		[self showDefaultFootViewWithHeight:height];
	}
	return self;
}
- (id)initWithBlankHeadView:(CGFloat)headViewHeight footView:(CGFloat)footViewHeight{
	if(self=[self init]){
		[self showDefaultHeadViewWithHeight:headViewHeight];
		[self showDefaultFootViewWithHeight:footViewHeight];
	}
	return self;
}
- (void)showDefaultHeadViewWithHeight:(CGFloat)height{
	self.showHeadView = YES;
	self.showDefaultHeadView = YES;
	self.headViewHeight = height;
}
- (void)showDefaultFootViewWithHeight:(CGFloat)height{
	self.showFootView = YES;
	self.showDefaultFootView = YES;
	self.footViewHeight = height;
}
- (void)displayHeadView:(UIView<MKUITableViewSectionViewProtocol> *)view{
	[view setSectionModel:self kind:MKUITableViewSectionViewKindOfHead];
	if(self.whenShowHeadView){
		self.whenShowHeadView(self,view);
	}
	[view setNeedsLayout];
}
- (void)displayFootView:(UIView<MKUITableViewSectionViewProtocol> *)view{
	[view setSectionModel:self kind:MKUITableViewSectionViewKindOfFoot];
	if(self.whenShowFootView){
		self.whenShowFootView(self,view);
	}
	[view setNeedsLayout];
}

- (void)refresh{
    [self refreshWithAnimated:NO];
}
- (void)refreshWithAnimated:(BOOL)animated{
    if (self.tableView) {
        for (MKUITableViewCellModel *cm in self.cellModels) {
            cm.needReloadCell = YES;
        }
        NSIndexSet *set = [self.tableViewModel indexSetOfSectionModel:self];
        if(set){
            [self.tableView reloadSections:set withRowAnimation:animated?UITableViewRowAnimationNone:UITableViewRowAnimationAutomatic];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            for (MKUITableViewCellModel *cm in self.cellModels) {
                if(cm.selected){
                    [self.tableView selectRowAtIndexPath:cm.indexPathInModel animated:animated scrollPosition:(UITableViewScrollPositionNone)];
                }
            }
        });
    }
}
#pragma debug
- (NSString *)description{
	return [NSString stringWithFormat:@"%@:[indexTitle:%@,showHeadView:%d,headTitle:%@,headViewHeight:%f,showFootView:%d,footTitle:%@,footViewHeight:%f,cells:%@,userInfo:%@]",NSStringFromClass(self.class),self.indexTitle,self.showHeadView,self.headTitle,self.headViewHeight,self.showFootView,self.footTitle,self.footViewHeight,self.cellModels,self.userInfo];
}
- (void)dealloc{
//	NSLog(@"deallocMKUITableViewSectionModel:%@", self);
}
@end
